﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingWorkshop.Model;
using TrainingWorkshop.DALFactory;
using TrainingWorkshop.IDAL;
using TrainingWorkShop.CommonUtility;

namespace TrainingWorkShop.BLL
{
    /// <summary>
    /// CourseDetail Business Unit
    /// </summary>
  public   class CourseDetailBU
    {

      /// <summary>
      /// UpdateCourseDetail Contain Insert and delete
      /// </summary>
      /// <param name="courseId">int CourseId</param>
      /// <param name="oldStudentList">old StudentList</param>
      /// <param name="newStudentList">New StudentList</param>
      public void UpdateCourseDetail(int courseId ,List<Student> oldStudentList,List<Student> newStudentList)
      {
          List<Student> studentsToDelete = new List<Student>();
          List<Student> studentsToAdd = new List<Student>();
          List<Student> studnetsToKeep = new List<Student>();
          StudentListEquality studentlistEqual = new StudentListEquality();
          
          //get students to Keep
          studnetsToKeep = oldStudentList.Intersect(newStudentList, studentlistEqual).ToList();
          //get students to Add
          studentsToAdd = newStudentList.Except(studnetsToKeep, studentlistEqual).ToList();
          //get students to Delete
          studentsToDelete = oldStudentList.Except(studnetsToKeep, studentlistEqual).ToList();
          //delete students from course
          DeleteStudentFromCourse(courseId,studentsToDelete);
         //add student into course
          AddStudentToCourse(courseId,studentsToAdd);
        
      }

      /// <summary>
      /// Add new student into given course
      /// </summary>
      /// <param name="courseId">given course</param>
      /// <param name="studentsToAdd">list of student to add</param>
      public void AddStudentToCourse(int courseId,List<Student> studentsToAdd)
      {
          IFactory factory = new ApplicationContext().CreateFactory("EF");
          ICourseDetailRepository courseDetailRep = factory.CreateCourseDetailRepository();

          if (studentsToAdd!=null&&studentsToAdd.Count>0)
          {
              foreach (Student s in studentsToAdd)
              {
                  courseDetailRep.Insert(courseId, s);
              } 
          }
      
      }

      /// <summary>
      /// Delete specify students from given course
      /// </summary>
      /// <param name="courseId">given courseId</param>
      /// <param name="studentsToDelete">list of student to delete</param>
      public void DeleteStudentFromCourse(int courseId, List<Student> studentsToDelete)
      {
          IFactory factory = new ApplicationContext().CreateFactory("EF");
          ICourseDetailRepository courseDetailRep = factory.CreateCourseDetailRepository();
          if (studentsToDelete != null&&studentsToDelete.Count>0)
          {
              foreach (Student s in studentsToDelete)
              {
                  courseDetailRep.Delete(courseId, s);
              }
          }
      }
    }
}
